﻿using System;
using System.Web.Mvc;
using iss.sg4210.se18.team6s.jass.BL;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.Utilities;

namespace iss.sg4210.se18.team6s.Controllers
{
    [Authorize(Roles = ViewConstants.ROLE_ADMIN + "," + ViewConstants.ROLE_JOB_SEEKER)]
    public class AlertCriteriaController : Controller
    {
        AlertCriteriaFacade alertCriteriaFacade = null;
        public AlertCriteriaController()
        {
            alertCriteriaFacade = new AlertCriteriaFacade();
        }
        //
        // GET: /AlertCriteria/

        public ActionResult Index()
        {
            if (Common.IsLoggedInUserAdmin())
                return View(alertCriteriaFacade.FindAllAlertCriterias());
            else
                return View(alertCriteriaFacade.FindAllAlertCriteriasByUser(Common.GetLoggedInUserId()));
        }

        //
        // GET: /AlertCriteria/Details/5

        public ActionResult Details(int id)
        {
            return View(alertCriteriaFacade.GetAlertCriteria(id));
        }

        //
        // GET: /AlertCriteria/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AlertCriteria/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                AlertCriteria alertCriteria = new AlertCriteria();
                UpdateModel(alertCriteria, collection);
                alertCriteria.UserId = Common.GetLoggedInUserId();
                alertCriteriaFacade.AddAlertCriteria(alertCriteria);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {

                return View();
            }
        }

        //
        // GET: /AlertCriteria/Edit/5

        public ActionResult Edit(int id)
        {
            return View(alertCriteriaFacade.GetAlertCriteria(id));
        }

        //
        // POST: /AlertCriteria/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            AlertCriteria alertCriteria = alertCriteriaFacade.GetAlertCriteria(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(alertCriteria);
                alertCriteriaFacade.UpdateAlertCriteria(alertCriteria);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(alertCriteria);
            }
            catch (Exception ex)
            {
                return View(alertCriteria);
            }
        }

        //
        // GET: /AlertCriteria/Delete/5

        public ActionResult Delete(int id)
        {
            return View(alertCriteriaFacade.GetAlertCriteria(id));
        }

        //
        // POST: /AlertCriteria/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                alertCriteriaFacade.DeleteAlertCriteria(id);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(alertCriteriaFacade.GetAlertCriteria(id));
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError(string.Empty, ex.Message);
                return View(alertCriteriaFacade.GetAlertCriteria(id));
            }
        }
    }
}
